﻿Imports DAL

Public Class BuQuotationDetails


    ''' <summary>
    ''' Truy vấn tất cả thông tin về Quotation Details và trả về một DataSet
    ''' </summary>
    ''' <param name="rowGuidQuotation">RowGuid tham chiếu của Quotation</param>
    Public Function GetQuotationDetailsInDataSet(ByVal rowGuidQuotation As Guid) As DataSet

        'FUNC: Kỹ thuật Binding một Hierrachy DataSet 
        Dim dsQuotationDetails As New DataSet

        Dim ienQuotationDetails As IEnumerable(Of spQuotationDetailsType)

        Using dbEntities As New PrestozEntities
            ienQuotationDetails = dbEntities.SpQuotationDetails(rowGuidQuotation)
        End Using

        'TẠO DATATABLE CHO THÔNG TIN QUOTATIONDETAILS

        'Khai báo DataTable QuotationDetails
        Dim dtQuotationDetails As New DataTable("QuotationDetails")

        'Khai báo mảng cột để thiết lập khóa chính cho bảng
        Dim pkOneColumn(0) As DataColumn
        Dim pkTwoColumns(1) As DataColumn

        'Khai báo một biến đối tượng DataColumn
        Dim dcQuotationDetails As DataColumn

        '0 Thêm cột RequestProductId vào DataTable
        dcQuotationDetails = New DataColumn("RequestProductId", GetType(String))
        dcQuotationDetails.MaxLength = 256
        dcQuotationDetails.Caption = "Mã sản phẩm"
        dtQuotationDetails.Columns.Add(dcQuotationDetails)

        '1 Thêm cột CatalogNumber vào DataTable
        dcQuotationDetails = New DataColumn("CatalogNumber", GetType(String))
        dcQuotationDetails.MaxLength = 256
        dcQuotationDetails.Caption = "CAT.No."
        dtQuotationDetails.Columns.Add(dcQuotationDetails)

        '2 Thêm cột RowGuidQuotationDetails vào DataTable
        dcQuotationDetails = New DataColumn("RowGuidQuotationDetails", GetType(Guid))
        dcQuotationDetails.Caption = "Mã Guid"
        dtQuotationDetails.Columns.Add(dcQuotationDetails)

        'Đưa cột vào nhóm khóa chinh
        pkOneColumn(0) = dcQuotationDetails

        'Thiết lập khóa chính cho bảng
        dtQuotationDetails.PrimaryKey = pkOneColumn

        '3 Thêm cột Quantity vào DataTable
        dcQuotationDetails = New DataColumn("Quantity", GetType(Int32))
        dcQuotationDetails.Caption = "Số lượng"
        dtQuotationDetails.Columns.Add(dcQuotationDetails)

        '4 Thêm cột RADiscount vào DataTable
        dcQuotationDetails = New DataColumn("RADiscount", GetType(Single))
        dcQuotationDetails.Caption = "Chiết khấu RA"
        dtQuotationDetails.Columns.Add(dcQuotationDetails)

        '5 Thêm cột SPRDiscount vào DataTable
        dcQuotationDetails = New DataColumn("SPRDiscount", GetType(Single))
        dcQuotationDetails.Caption = "Chiết khấu SPR"
        dtQuotationDetails.Columns.Add(dcQuotationDetails)

        '6 Thêm cột EGDiscount vào DataTable
        dcQuotationDetails = New DataColumn("EGDiscount", GetType(Int32))
        dcQuotationDetails.Caption = "Chiết khấu NPP"
        dtQuotationDetails.Columns.Add(dcQuotationDetails)


        'TẠO DATATABLE CHO THÔNG TIN PRICELIST

        'Khai báo DataTable PriceList
        Dim dtPriceList As New DataTable("PriceList")

        '0 Thêm cột CatalogNumber vào DataTable
        dcQuotationDetails = New DataColumn("CatalogNumber", GetType(String))
        dcQuotationDetails.MaxLength = 256
        dcQuotationDetails.Caption = "CAT.No."
        dtPriceList.Columns.Add(dcQuotationDetails)

        'Đưa cột vào nhóm khóa chính
        pkTwoColumns(0) = dcQuotationDetails

        '1 Thêm cột Description vào DataTable
        dcQuotationDetails = New DataColumn("Description", GetType(String))
        dcQuotationDetails.Caption = "Mô tả"
        dtPriceList.Columns.Add(dcQuotationDetails)

        '2 Thêm cột DS vào DataTable
        dcQuotationDetails = New DataColumn("DS", GetType(String))
        dcQuotationDetails.MaxLength = 10
        dcQuotationDetails.Caption = "DS"
        dtPriceList.Columns.Add(dcQuotationDetails)

        '3 Thêm cột RowGuidQuotationDetails vào DataTable
        dcQuotationDetails = New DataColumn("RowGuidQuotationDetails", GetType(Guid))
        dcQuotationDetails.Caption = "Mã Guid"
        dtPriceList.Columns.Add(dcQuotationDetails)

        'Đưa cột vào nhóm khóa chính
        pkTwoColumns(1) = dcQuotationDetails

        'Thiết lập khóa chính cho bảng
        dtPriceList.PrimaryKey = pkTwoColumns

        '4 Thêm cột PGC vào DataTable
        dcQuotationDetails = New DataColumn("PGC", GetType(String))
        dcQuotationDetails.MaxLength = 10
        dcQuotationDetails.Caption = "PGC"
        dtPriceList.Columns.Add(dcQuotationDetails)

        '5 Thêm cột PF vào DataTable
        dcQuotationDetails = New DataColumn("PF", GetType(String))
        dcQuotationDetails.MaxLength = 10
        dcQuotationDetails.Caption = "PF"
        dtPriceList.Columns.Add(dcQuotationDetails)

        '6 Thêm cột ListPrice vào DataTable
        dcQuotationDetails = New DataColumn("ListPrice", GetType(String))
        dcQuotationDetails.Caption = "Giá PriceList"
        dtPriceList.Columns.Add(dcQuotationDetails)

        '7 Thêm cột ListPrice vào DataTable
        dcQuotationDetails = New DataColumn("MinQty", GetType(UInt16))
        dcQuotationDetails.Caption = "MinQty"
        dtPriceList.Columns.Add(dcQuotationDetails)

        '8 Thêm cột DDD vào DataTable
        dcQuotationDetails = New DataColumn("DDD", GetType(Single))
        dcQuotationDetails.Caption = "DDD"
        dtPriceList.Columns.Add(dcQuotationDetails)

        '9 Thêm cột SEU vào DataTable
        dcQuotationDetails = New DataColumn("SEU", GetType(Single))
        dcQuotationDetails.Caption = "SEU"
        dtPriceList.Columns.Add(dcQuotationDetails)

        '10 Thêm cột LEU vào DataTable
        dcQuotationDetails = New DataColumn("LEU", GetType(Single))
        dcQuotationDetails.Caption = "LEU"
        dtPriceList.Columns.Add(dcQuotationDetails)

        '11 Thêm cột SOEM vào DataTable
        dcQuotationDetails = New DataColumn("SOEM", GetType(Single))
        dcQuotationDetails.Caption = "SOEM"
        dtPriceList.Columns.Add(dcQuotationDetails)

        '12 Thêm cột LOEM vào DataTable
        dcQuotationDetails = New DataColumn("LOEM", GetType(Single))
        dcQuotationDetails.Caption = "LOEM"
        dtPriceList.Columns.Add(dcQuotationDetails)

        '13 Thêm cột SI vào DataTable
        dcQuotationDetails = New DataColumn("SI", GetType(Single))
        dcQuotationDetails.Caption = "SI"
        dtPriceList.Columns.Add(dcQuotationDetails)

        '14 Thêm cột DsPgcNoCustomerMultiplier vào DataTable
        dcQuotationDetails = New DataColumn("DsPgcNoCustomerMultiplier", GetType(Single))
        dcQuotationDetails.Caption = "CM A"
        dtPriceList.Columns.Add(dcQuotationDetails)

        '15 Thêm cột DsPgcNoDistributorMultiplier vào DataTable
        dcQuotationDetails = New DataColumn("DsPgcNoDistributorMultiplier", GetType(Single))
        dcQuotationDetails.Caption = "DM A"
        dtPriceList.Columns.Add(dcQuotationDetails)

        '16 Thêm cột DsPgcNoDDD vào DataTable
        dcQuotationDetails = New DataColumn("DsPgcNoDDD", GetType(Single))
        dcQuotationDetails.Caption = "DDD A"
        dtPriceList.Columns.Add(dcQuotationDetails)

        '17 Thêm cột DsPgcCustomerMultiplier vào DataTable
        dcQuotationDetails = New DataColumn("DsPgcCustomerMultiplier", GetType(Single))
        dcQuotationDetails.Caption = "CM B"
        dtPriceList.Columns.Add(dcQuotationDetails)

        '18 Thêm cột DsPgcDistributorMultiplier vào DataTable
        dcQuotationDetails = New DataColumn("DsPgcDistributorMultiplier", GetType(Single))
        dcQuotationDetails.Caption = "DM B"
        dtPriceList.Columns.Add(dcQuotationDetails)

        '19 Thêm cột DsPgcDDD vào DataTable
        dcQuotationDetails = New DataColumn("DsPgcDDD", GetType(Single))
        dcQuotationDetails.Caption = "DDD B"
        dtPriceList.Columns.Add(dcQuotationDetails)

        '20 Thêm cột DsCustomerMultiplier vào DataTable
        dcQuotationDetails = New DataColumn("DsCustomerMultiplier", GetType(Single))
        dcQuotationDetails.Caption = "CM C"
        dtPriceList.Columns.Add(dcQuotationDetails)

        '21 Thêm cột DsDistributorMultiplier vào DataTable
        dcQuotationDetails = New DataColumn("DsDistributorMultiplier", GetType(Single))
        dcQuotationDetails.Caption = "DM C"
        dtPriceList.Columns.Add(dcQuotationDetails)

        '22 Thêm cột DsDDD vào DataTable
        dcQuotationDetails = New DataColumn("DsDDD", GetType(Single))
        dcQuotationDetails.Caption = "DDD C"
        dtPriceList.Columns.Add(dcQuotationDetails)

        '23 Thêm cột ImportTax vào DataTable
        dcQuotationDetails = New DataColumn("ImportTax", GetType(Single))
        dcQuotationDetails.Caption = "Import Tax"
        dtPriceList.Columns.Add(dcQuotationDetails)

        'Khai báo một biến đối tượng DataRow
        Dim drQuotationDetails As DataRow


        'FUNC: Kỹ thuật lặp xuyên IEnumerable(Of T)
        For Each oQuotationDetailsType As spQuotationDetailsType In ienQuotationDetails

            'CẤP DỮ LIỆU CHO BẢNG QUOTATIONDETAILS
            '-------------------------------------

            'Làm tươi lại đối tượng DataRow
            drQuotationDetails = dtQuotationDetails.NewRow()

            '0: RequestProductId
            drQuotationDetails("RequestProductId") = oQuotationDetailsType.RequestProductId

            '1: CatalogNumber
            drQuotationDetails("CatalogNumber") = oQuotationDetailsType.CatalogNumber

            '2: RowGuidQuotationDetails
            drQuotationDetails("RowGuidQuotationDetails") = oQuotationDetailsType.RowGuidQuotationDetails

            '3: Quantity
            drQuotationDetails("Quantity") = oQuotationDetailsType.Quantity

            '4: RADiscount
            drQuotationDetails("RADiscount") = oQuotationDetailsType.RADiscount

            '5: SPRDiscount
            drQuotationDetails("SPRDiscount") = oQuotationDetailsType.SPRDiscount

            '6: EGDiscount
            drQuotationDetails("EGDiscount") = oQuotationDetailsType.EGDiscount

            'Thêm hàng vào DataTable
            dtQuotationDetails.Rows.Add(drQuotationDetails)


            'CẤP DỮ LIỆU CHO BẢNG PRICELIST
            '-------------------------------

            'Làm tươi lại đối tượng DataRow
            drQuotationDetails = dtPriceList.NewRow()

            '0: CatalogNumber
            drQuotationDetails("CatalogNumber") = oQuotationDetailsType.CatalogNumber

            '1: Description
            drQuotationDetails("Description") = oQuotationDetailsType.Description

            '2: DS
            drQuotationDetails("DS") = oQuotationDetailsType.DS

            '3: RowGuidQuotationDetails
            drQuotationDetails("RowGuidQuotationDetails") = oQuotationDetailsType.RowGuidQuotationDetails

            '4: PGC
            drQuotationDetails("PGC") = oQuotationDetailsType.PGC

            '5: PF
            drQuotationDetails("PF") = oQuotationDetailsType.PF

            '6: ListPrice
            drQuotationDetails("ListPrice") = oQuotationDetailsType.ListPrice

            '7: MinQty
            drQuotationDetails("MinQty") = oQuotationDetailsType.MinQty

            '8: DDD
            drQuotationDetails("DDD") = oQuotationDetailsType.DDD

            '9: SEU
            drQuotationDetails("SEU") = oQuotationDetailsType.SEU

            '10: LEU
            drQuotationDetails("LEU") = oQuotationDetailsType.LEU

            '11: SOEM
            drQuotationDetails("SOEM") = oQuotationDetailsType.SOEM

            '12: LOEM
            drQuotationDetails("LOEM") = oQuotationDetailsType.LOEM

            '13: SI
            drQuotationDetails("SI") = oQuotationDetailsType.SI

            '14: DsPgcNoCustomerMultiplier
            If oQuotationDetailsType.DsPgcNoCustomerMultiplier IsNot Nothing Then
                drQuotationDetails("DsPgcNoCustomerMultiplier") = oQuotationDetailsType.DsPgcNoCustomerMultiplier
            Else
                drQuotationDetails("DsPgcNoCustomerMultiplier") = DBNull.Value
            End If


            '15: DsPgcNoDistributorMultiplier
            If oQuotationDetailsType.DsPgcNoDistributorMultiplier IsNot Nothing Then
                drQuotationDetails("DsPgcNoDistributorMultiplier") = oQuotationDetailsType.DsPgcNoDistributorMultiplier
            Else
                drQuotationDetails("DsPgcNoDistributorMultiplier") = DBNull.Value
            End If

            '16: DsPgcNoDDD
            If oQuotationDetailsType.DsPgcNoDDD IsNot Nothing Then
                drQuotationDetails("DsPgcNoDDD") = oQuotationDetailsType.DsPgcNoDDD
            Else
                drQuotationDetails("DsPgcNoDDD") = DBNull.Value
            End If


            '17: DsPgcCustomerMultiplier
            If oQuotationDetailsType.DsPgcCustomerMultiplier IsNot Nothing Then
                drQuotationDetails("DsPgcCustomerMultiplier") = oQuotationDetailsType.DsPgcCustomerMultiplier
            Else
                drQuotationDetails("DsPgcCustomerMultiplier") = DBNull.Value
            End If


            '18: DsPgcDistributorMultiplier
            If oQuotationDetailsType.DsPgcDistributorMultiplier IsNot Nothing Then
                drQuotationDetails("DsPgcDistributorMultiplier") = oQuotationDetailsType.DsPgcDistributorMultiplier
            Else
                drQuotationDetails("DsPgcDistributorMultiplier") = DBNull.Value
            End If


            '19: DsPgcDDD
            If oQuotationDetailsType.DsPgcDDD IsNot Nothing Then
                drQuotationDetails("DsPgcDDD") = oQuotationDetailsType.DsPgcDDD
            Else
                drQuotationDetails("DsPgcDDD") = DBNull.Value
            End If


            '20: DsCustomerMultiplier
            If oQuotationDetailsType.DsCustomerMultiplier IsNot Nothing Then
                drQuotationDetails("DsCustomerMultiplier") = oQuotationDetailsType.DsCustomerMultiplier
            Else
                drQuotationDetails("DsCustomerMultiplier") = DBNull.Value
            End If


            '21: DsDistributorMultiplier
            If oQuotationDetailsType.DsDistributorMultiplier IsNot Nothing Then
                drQuotationDetails("DsDistributorMultiplier") = oQuotationDetailsType.DsDistributorMultiplier
            Else
                drQuotationDetails("DsDistributorMultiplier") = DBNull.Value
            End If


            '22: DsDDD
            If oQuotationDetailsType.DsDDD IsNot Nothing Then
                drQuotationDetails("DsDDD") = oQuotationDetailsType.DsDDD
            Else
                drQuotationDetails("DsDDD") = DBNull.Value
            End If

            '23: Import
            If Not Single.IsNaN(oQuotationDetailsType.ImportTax) Then
                drQuotationDetails("ImportTax") = oQuotationDetailsType.ImportTax
            Else
                drQuotationDetails("ImportTax") = 0.15
            End If

            'Thêm hàng vào DataTable
            dtPriceList.Rows.Add(drQuotationDetails)
        Next


        'Đặt các DataTable vào DataSet
        dsQuotationDetails.Tables.Add(dtQuotationDetails)
        dsQuotationDetails.Tables.Add(dtPriceList)

        'THIẾT LẬP KHÓA NGOẠI GIỮA CÁC BẢNG

        'FK giữa QuotationDetails và PriceList


        Try
            Dim relation As New DataRelation("FK_QuotationDetails_PriceList",
                                             dsQuotationDetails.Tables("QuotationDetails").Columns("CatalogNumber"),
                                             dsQuotationDetails.Tables("PriceList").Columns("CatalogNumber"))
            dsQuotationDetails.Relations.Add(relation)
        Catch ex As Exception
            'Do Nothing
        End Try

        Return dsQuotationDetails
    End Function


    ''' <summary>
    ''' Truy vấn tất cả thông tin về Quotation Details và trả về một ArrayList
    ''' </summary>
    ''' <param name="rowGuidQuotation">RowGuid tham chiếu của Quotation</param>
    Public Function GetQuotationDetailsInList(ByVal rowGuidQuotation As Guid) As List(Of spQuotationDetailsType)

        Using dbEntities As New PrestozEntities
            Return dbEntities.SpQuotationDetails(rowGuidQuotation).ToList()
        End Using

    End Function


    Public Function UpdateQuotationDetailsInDataSet(ByVal dsOldData As DataSet, ByVal alNewData As ArrayList) As DataSet




    End Function


End Class
